package com.ymtms.arrivalofgoods.controller;

import com.github.pagehelper.PageInfo;
import com.netflix.discovery.converters.Auto;
import com.ymtms.arrivalofgoods.bo.GridRequest;
import com.ymtms.arrivalofgoods.bo.GridResult;
import com.ymtms.arrivalofgoods.constant.ArriveConstant;
import com.ymtms.arrivalofgoods.exception.ArriveException;
import com.ymtms.arrivalofgoods.pojo.DriverInformation;
import com.ymtms.arrivalofgoods.pojo.GoodsbillEvent;
import com.ymtms.arrivalofgoods.pojo.GoodsbillMaster;
import com.ymtms.arrivalofgoods.pojo.GoodsbillReceoptMain;
import com.ymtms.arrivalofgoods.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
public class GoodsbillReceoptMainController {
    @Autowired
    private GoodsbillReceoptMainService goodsbillReceoptMainService;
    @Autowired
    private DriverInformationService driverInformationService;
    @Autowired
    private  GoodsbillEventService goodsbillEventService;
    @Autowired
    private  GoodsbillMasterService goodsbillMasterService;
    @Autowired
    private  CheckGoodsReceiveService checkGoodsReceiveService;

    @ResponseBody
    @RequestMapping("checkGoods")
    public void checkGoods(Integer[] grmid, String status) {


    }

    @ResponseBody
    @RequestMapping("/addReceipt")
    public Map insertOneReceipt(String goodsBillCode) {
        Map map = new HashMap();
        try {
            goodsbillReceoptMainService.addReceoptMain(goodsBillCode);
            map.put("code", "0");
            map.put("msg", "新增成功");
        } catch (ArriveException e) {
            map.put("code", "600");
            map.put("msg", e.getMessage());
        }
        return map;
    }
/**
*Discription Todo
*@Param 接货并验收
*@return 
*@author Administrator
*@date 2019/11/4 11:11
**/
    @ResponseBody
    @RequestMapping("/addReceiptAccept")
    public Map addReceiptAccept(String goodsBillCode) {
        Map map = new HashMap();
        try {
            goodsbillReceoptMainService.addReceoptMain(goodsBillCode);
            GoodsbillMaster masterByBillcode = goodsbillMasterService.findMasterByBillcode(goodsBillCode);
            GoodsbillReceoptMain receiptMainByPkid = goodsbillReceoptMainService.findReceiptMainByPkid(masterByBillcode.getGmid());
            checkGoodsReceiveService.updateByReceiptId(receiptMainByPkid.getGrmid());
            map.put("code", "0");
            map.put("msg", "新增成功");
        } catch (ArriveException e) {
            map.put("code", "600");
            map.put("msg", e.getMessage());
        }
        return map;
    }
/**
*Discription Todo找到所有的未到订单
*@Param
*@return
*@author Administrator
*@date 2019/11/4 11:08
**/
    @ResponseBody
    @RequestMapping("/findAllEvent")
    public Map findAllEvent() {
        Map map = new HashMap();
        try {
            List<GoodsbillEvent> eventData = goodsbillEventService.findAllUnarrivedData();
            map.put("eventData",eventData);
            map.put("code", "0");
            map.put("msg", "成功");
        } catch (ArriveException e) {
            map.put("code", "600");
            map.put("msg", "失败");
        }
        return map;
    }


    @ResponseBody
    @RequestMapping("/query")
    public Map queryReceipt(@RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                            @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                            @RequestParam(value = "goodsbillReceoptMain", required = false) GoodsbillReceoptMain goodsbillReceoptMain,
                            @RequestParam(value = "arriveBegin", required = false) Date arriveBegin,
                            @RequestParam(value = "arriveEnd", required = false) Date arriveEnd,
                            @RequestParam(value = "linkmanPhone", required = false) String linkmanPhone) {
        try {
            Map map = new HashMap();
            //System.out.println(arriveBegin+"--->>>"+arriveEnd);
            System.out.println("gggggggggggggggggggggggggggg");
            List<GoodsbillReceoptMain> list = goodsbillReceoptMainService.findAllGoodsbillReceiptByCondition(currentPage, pageSize, goodsbillReceoptMain, arriveBegin, arriveEnd, linkmanPhone);
            PageInfo<GoodsbillReceoptMain> pageInfo = new PageInfo<>(list);
            map.put("pageInfo", pageInfo);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }


    @ResponseBody
    @RequestMapping("/seek")
    public GridResult<GoodsbillReceoptMain> seekReceipt(
            GridRequest gridRequest, // 封装页面中laiyu表格所传过来的基本信息
            GoodsbillReceoptMain goodsbillReceoptMain,
            @RequestParam(value = "idcard", required = false) String idcard,
            @RequestParam(value = "arriveBegin", required = false) Date arriveBegin,
            @RequestParam(value = "arriveEnd", required = false) Date arriveEnd,
            @RequestParam(value = "linkmanPhone", required = false) String linkmanPhone) {
        try {
            System.out.println("gggggggggggggggggggggggggggg");
// 获取当前页和页大小
            //System.out.println(idcard);
            if (idcard != null && idcard != "") {
                DriverInformation driver = driverInformationService.findDriverByIdcard(idcard);
                // System.out.println(driver);
                goodsbillReceoptMain.setDriverPkid(driver.getDiid());
            }
            //System.out.println(goodsbillReceoptMain.getGoodsRevertBillCode());

            Integer currentPage = gridRequest.getPage();
            Integer pageSize = gridRequest.getLimit();


            List<GoodsbillReceoptMain> list = goodsbillReceoptMainService.findAllGoodsbillReceiptByCondition(currentPage, pageSize, goodsbillReceoptMain, arriveBegin, arriveEnd, linkmanPhone);
            PageInfo<GoodsbillReceoptMain> pageInfo = new PageInfo<>(list);
            GridResult<GoodsbillReceoptMain> grid = new GridResult<>();

            // 存入查询数据
            grid.setData(list);
            // 存入总条数
            grid.setCount(pageInfo.getTotal());
            grid.setCode(0);
            grid.setMsg("");
            return grid;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }


    /**
     * Discription Todo删除指定id
     *
     * @return
     * @Param
     * @author Administrator
     * @date 2019/10/29 15:38
     **/


    @PostMapping("/deleteBatch/{keys}")
    @ResponseBody
    public Map delete(@PathVariable("keys") Integer[] keys) {
        Map result = new HashMap();
        try {
            goodsbillReceoptMainService.deleteReceiptByKeys(keys); // 批量删除
            // 清空指定的redis缓存
//            Set<String> key = redisTemplate.keys("goodsbillMaster::*");
//            redisTemplate.delete(key);
            result.put("code", "0");
            result.put("msg", "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", "500");
            result.put("msg", "删除失败");
        }
        return result;
    }

    /**
     * Discription Todo验收货物
     *
     * @return
     * @Param
     * @author Administrator
     * @date 2019/10/21 12:17
     **/
    @ResponseBody
    @RequestMapping("/accept/{grmid}")
    public Map acceptGoods(@PathVariable("grmid") Integer grmid) {
        Map map = new HashMap();
        try {
            goodsbillReceoptMainService.acceptGoods(grmid);
            map.put("code", "0");
            map.put("msg", "客户收货成功");
        } catch (Exception e) {
            map.put("code", "500");
            map.put("msg", "收货失败");
            e.printStackTrace();
        }

        return map;
    }

    /**
     * Discription Todo找到所有未验收的数据
     *
     * @return
     * @Param
     * @author Administrator
     * @date 2019/10/24 9:34
     **/

    @ResponseBody
    @RequestMapping("/findReceipt")
    public Map findAllUnsettledReceipt() {
        Map map = new HashMap();
        try {
            List allUnsettledReceipt = goodsbillReceoptMainService.findAllUnsettledReceipt();
            map.put("code", "0");
            map.put("msg", "成功");
            map.put("list", allUnsettledReceipt);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", "500");
            map.put("msg", "失败");
        }
        return map;

    }

    @ResponseBody
    @RequestMapping("/modifyReceipt")
    public Map modifyReceipt(GoodsbillReceoptMain goodsbillReceoptMain) {
        System.out.println(goodsbillReceoptMain);

        Map map = new HashMap();
        try {
            //System.out.println(goodsbillReceoptMain.getLinkmanPhone());
            goodsbillReceoptMainService.modifyOneReceipt(goodsbillReceoptMain);
            map.put("code", "0");
            map.put("msg", "成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", "500");
            map.put("msg", "失败");
        }
        return map;

    }







    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        //转换日期 注意这里的转化要和传进来的字符串的格式一直 如2015-9-9 就应该为yyyy-MM-dd
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
    }
}
